import pandas as pd
import copy

from learner.preprocess import normalize
from learner.preprocess import get_ptx_types, get_inst_types, get_mem_types
from learner.models import nn_fitting, xg_fitting, mean_absolute_percentage_error

# keys = ['coreF', 'SMACT', 'SMOCC', 'TENSO', 'DRAMA', 'FP64A', 'FP32A', 'FP16A', 'SMACT_single_2', 'SMOCC_single_2', 'TENSO_single_2', 'DRAMA_single_2', 'FP64A_single_2', 'FP32A_single_2', 'FP16A_single_2']
# keys = ['coreF', 'power_single_1', 'power_single_2']
# keys = ['coreF', 'time/ms', 'time/ms_2', 'time/ms_single_1', 'time/ms_single_2', 'SMACT', 'DRAMA', 'FP64A', 'FP32A', 'SMACT_single_2', 'DRAMA_single_2', 'FP64A_single_2', 'FP32A_single_2']
power_keys = ['coreF', 'SMACT', 'DRAMA', 'FP64A', 'FP32A', 'SMACT_single_2', 'DRAMA_single_2', 'FP64A_single_2', 'FP32A_single_2', 'power_single_1', 'power_single_2']
perf_keys  = ['coreF', 'SMACT', 'DRAMA', 'FP64A', 'FP32A', 'SMACT_single_2', 'DRAMA_single_2', 'FP64A_single_2', 'FP32A_single_2', 't1_vs_t2']

if __name__ == '__main__':

    train_data = pd.read_csv('mb_normal.csv', header=0)
    test_data = pd.read_csv('real_normal.csv', header=0)

    train_data['coreF'] = train_data['coreF'] / 1380
    train_data['power_single_1'] = train_data['power_single_1'] / 250000
    train_data['power_single_2'] = train_data['power_single_2'] / 250000
    train_data['power'] = train_data['power'] / 250000
    train_data['t1_vs_t2'] = train_data['time/ms'] / train_data['time/ms_2']

    test_data['coreF'] = test_data['coreF'] / 1380
    test_data['power_single_1'] = test_data['power_single_1'] / 250000
    test_data['power_single_2'] = test_data['power_single_2'] / 250000
    test_data['power'] = test_data['power'] / 250000
    test_data['t1_vs_t2'] = test_data['time/ms'] / test_data['time/ms_2']

    # t = train_data['time/ms_single_1'].tolist()
    # dvfs_ratio1 = [t[i] / t[i // 7 * 7 + 3] for i in range(len(t))]
    # train_X['dvfs_ratio1'] = dvfs_ratio1
    # t = train_data['time/ms_single_2'].tolist()
    # dvfs_ratio2 = [t[i] / t[i // 7 * 7 + 3] for i in range(len(t))]
    # train_X['dvfs_ratio2'] = dvfs_ratio2

    train_X = train_data[perf_keys]
    train_y = train_data['ratio1']

    test_X = test_data[perf_keys]
    test_y = test_data['ratio1']

    print(train_X.head(7))
    print(train_y.head(7))

    model = nn_fitting(train_X, train_y)
    #model = xg_fitting(train_X, train_y)

    train_y_pred = model.predict(train_X).flatten()
    train_mae = mean_absolute_percentage_error(train_y, train_y_pred)
    print(train_mae)

    test_y_pred = model.predict(test_X).flatten()
    test_mae = mean_absolute_percentage_error(test_y, test_y_pred)
    print(test_mae)

